home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9712 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  98 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: malloc question
  5. Date: Tue, 12 Mar 96 19:31:36 GMT
  6. Organization: none
  7. Message-ID: <826659096snz@genesis.demon.co.uk>
  8. References: <4htonk$350@news.hklink.net> <4i492m$9jl@news1.warwick.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4i492m$9jl@news1.warwick.net>
  15.            acorn@warwick.net "Peter Kohlberger" writes:
  16.  
  17. >alex@station.net (Alex Chu) wrote:
  18. >>Hi everybody,
  19. >
  20. >>I have a question for the following snip C program.
  21. >
  22. >>typedef struct item {
  23. >>  int val;
  24. >>  struct item *next;
  25. >>} ITEM, *PITEM;
  26. >
  27. >>main()
  28. >>{
  29. >>  PITEM head, current;
  30. >>  head=(PITEM) malloc(sizeof(ITEM));
  31. >>            ^^^^^^^
  32. >>  head->val=1;
  33. >>}
  34.  
  35. >Without the cast, you can not convert *void into *ITEM;
  36.  
  37. You can if you are using the C language - the compiler must convert
  38. any pointer (other than a pointer to a function) to and from a pointer to
  39. void implicitly where the context demands it (almost, there are rules about
  40. const/volatile qualifiers that have to be met).
  41.  
  42.  with the cast,
  43. >you are allowed to do the conversion.  If the conversion is allowed in
  44. >one manner, why is it not allowed in the other manner?
  45. >
  46. >The answer is (mostly) so that the compiler can catch your errors.
  47. >While a pointer to void can be converted to a pointer to any type of
  48. >object, it's desirable to ensure that the conversion is the actual
  49. >intent of the programmer.  
  50.  
  51. What you describe here is true for pointers in general (and notably for
  52. char * which took on the role of 'generic' pointer before the introduction
  53. of void *) but not true for void * in particular.
  54.  
  55. ...
  56.  
  57. >If the above call to malloc() without a cast was allowed, head2 would
  58. >be allocated 6 bytes when it needs 46 bytes (in my environment).
  59. >Requiring the cast changes the code to:
  60. >
  61. >  head2 =(PITEM1)malloc(sizeof(ITEM1)); 
  62.  
  63. All this guarantees is that the type of the cast matches the type of the
  64. pointer being assigned to. Since the cast is otherwise unconstrained this
  65. doesn't help you much. For instance the compiler wouls quite happily accept:
  66.  
  67.   head2 =(PITEM2)malloc(sizeof(ITEM1)); 
  68.  
  69. You could try to improve on this by using a macro e.g.
  70.  
  71. #define malloc2(nelems, type)  ((type *)malloc((nelems) * sizeof(type))))
  72.  
  73. Unfortunately the preprocessor can't handle general type derivations and this
  74. fails if you pass the type as, say, char [8]. Another approach would be to:
  75.  
  76. #define malloc3(ptr, nelems) ((ptr) = malloc((nelems) * sizeof *(ptr))
  77.  
  78. which works because a cast is not necessary. The main drawback here is that
  79. it looks like a function call in usage but doesn't act like one (i.e. it
  80. modifies an argument). If that wasn't acceptable then you could use:
  81.  
  82. #define malloc4(ptr, nelems) (*(ptr) = malloc((nelems) * sizeof **(ptr))
  83.  
  84. and write, say,
  85.  
  86.     int    *ip;
  87.  
  88.     if (malloc4(&ip, arraysize) == NULL) { /*...*/ }
  89.  
  90. Quite neat but it could throw you in code you weren't familiar with. It would
  91. be good as part of the standard however.
  92.  
  93. -- 
  94. -----------------------------------------
  95. Lawrence Kirby | fred@genesis.demon.co.uk
  96. Wilts, England | 70734.126@compuserve.com
  97. -----------------------------------------
  98.